const app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    shopId:0,
    shopData:"",
    sortType: 1, //排序
    page:1,
    loadingStatus:false,
    msgStatus:false
  },
  toggleCt(){
    let that = this;
    if (that.data.shopData.is_ct == 0) { //0 没有收藏
      wx.showLoading({
        title: '正在请求操作',
      })
      wx.request({
        url: app.buildUrl('/user/add-collection'),
        header: app.buildHeader(),
        data: {
          id: that.data.shopId,
          type: 1
        },
        success(res) {
          wx.hideLoading()
          app.showToast(res.data.msg)
          if (res.data.code == 200) {
            that.setData({
              'shopData.is_ct': 1,
              'shopData.count': ++that.data.shopData.count
            })
          } else {
            app.showToast(res.data.msg)
          }
        }
      })
    } else if(that.data.shopData.is_ct == 1) { //1 已收藏
      wx.showModal({
        title: '提示',
        content: '确定要取消收藏？',
        success(res) {
          if (res.confirm) {
            wx.showLoading({
              title: '正在请求操作',
            })
            wx.request({
              url: app.buildUrl('/user/del-collection'),
              header: app.buildHeader(),
              data: {
                id: that.data.shopId,
                type:1
              },
              success(res) {
                wx.hideLoading()
                app.showToast(res.data.msg)
                if (res.data.code == 200) {
                  that.setData({
                    'shopData.is_ct': 0,
                    'shopData.count': --that.data.shopData.count
                  })
                } else {
                  app.showToast(res.data.msg)
                }
              }
            })
          }
        }
      })
    }
  },
  // 排序
  sortList(e){
    let that = this;
    let sortType = e.target.dataset.type;
    let thatSortType = that.data.sortType;
    that.setData({
      msgStatus: false
    })
    if (sortType == 1 || sortType == 2){
      if (sortType == thatSortType){
        wx.hideLoading();
        return;
      }else{
        that.setData({
          sortType: sortType
        })
      }
    } else if (sortType == 3){
      if (thatSortType == 3){
        that.setData({
          sortType:4
        })
      } else {
        that.setData({
          sortType: 3
        })
      }
    }
    wx.showLoading({
      title: '排序中...',
    })
    wx.request({
      url: app.buildUrl('/store/index'),
      header: app.buildHeader(),
      data: {
        page: 1,
        shop_id: that.data.shopId,
        type: that.data.sortType
      },
      success(res) {
        wx.hideLoading();
        if (res.data.code == 200) {
          that.setData({
            shopData: res.data.data,
            page: 1
          })
        } else {
          app.showToast(res.data.msg)
        }
      }
    })
  },
  // 加载更多
  load(){
    let that = this;
    if (that.data.loadingStatus) { return; }
    if (that.data.msgStatus) { return; }
    that.setData({
      loadingStatus: true
    })
    let page = that.data.page;
    page = ++page;
    wx.showLoading({
      title: '正在加载更多',
    })
    wx.request({
      url: app.buildUrl('/store/index'),
      header: app.buildHeader(),
      data: {
        page: page,
        shop_id: that.data.shopId,
        type: that.data.sortType
      },
      success(res) {
        wx.hideLoading()
        if (res.data.code == 200) {
          if (res.data.data.data.length == 0) {
            app.showToast('没有更多了')
            that.setData({
              msgStatus:true
            })
          } else {
            that.setData({
              'shopData.data': that.data.shopData.data.concat(res.data.data.data),
              msgStatus: false
            })
          }
        } else {
          page = --page;
          app.showToast(res.data.msg)
        }
        that.setData({
          page: page,
          loadingStatus: false
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this;
    that.setData({
      shopId: options.id
    })
    wx.showLoading({
      title: '加载列表...',
    })
    wx.request({
      url: app.buildUrl('/store/index'),
      header: app.buildHeader(),
      data:{
        page: 1,
        shop_id: that.data.shopId,
        type: that.data.sortType
      },
      success(res){
        wx.hideLoading();
        if (res.data.code == 200){
          that.setData({
            shopData: res.data.data
          })
        }else{
          app.showToast(res.data.msg)
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    
  }
})